package org.addressBook.utils;

import java.sql.*;

// JDBC工具类 封装驱动 建立链接 关闭
public class JdbcUtils {
    private static String driver = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/address_book?serverTimezone=UTC";
    private static String user = "senguang";
    private static String password = "2055";

    // 加载驱动 建立链接
    public static Connection getCon() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection con = DriverManager.getConnection(url, user, password);
        return con;
    }

    // 关闭链接 回收资源
    public static void close(ResultSet rs, Statement stm, Connection con) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (stm != null) {
            stm.close();
        }
        if (con != null) {
            con.close();
        }
    }
}
